using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

using Net.Fan.SmallPig.DataAccess;
using Net.Fan.SmallPig.BusinessRules;
using Net.Fan.SmallPig.Common;

public partial class Comm_ShowAttachment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Params["fileNo"] == null)
        {
            Response.Write("Param error!");
            Response.End();
            return;
        }

        int fileNo;
        int.TryParse(Request.Params["fileNo"], out fileNo);

        //this.Title = string.Format("Show File {0}", fileNo);

        long bugNo = FileDA.GetBugNo(fileNo.ToString());

        if (IsPostBack == false)
        {
            //PagePermission.CheckShowBugPermission(this, bugNo);
        }

        DataRow dr = FileDA.GetFile(fileNo.ToString());

        //Response.ContentType = ""; //dr["PersonImageType"]
        if (dr == null)
        {
            Response.Write("Sorry,This file is not exist.");
            Response.End();
        }
        else
        {
            //byte[] body = dr["fileBody"] as byte[];  
            string path = Config.Instance.DirectoryOfUpload + (dr["fileName"] as string);

            FileInfo fin = new FileInfo(path);

            if (fin.Exists == false)
            {
                Response.Write("Sorry,This file is not exist in directory.");
                Response.End();
            }

            Response.Clear();
            Response.ClearHeaders();
            Response.Buffer = false;
            Response.ContentType = "application/octet-stream";
            Response.AppendHeader("Connection", "keep-alive");
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(path), System.Text.Encoding.UTF8));
            Response.AppendHeader("Content-Length", fin.Length.ToString());

            Response.WriteFile(path);

        }
    }
}
